package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 网关 跨域配置类
 * @author aaronmegs
 * @create 2020-02-18 10:24 下午
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){

        // cors跨域配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("*"); //允许哪里域名实现跨域  * 所有
        configuration.setAllowCredentials(true);//允许跨域的请求  能否携带用户信息
        configuration.addAllowedMethod("*");//允许跨域请求的方法 * 所有方法
        configuration.addAllowedHeader("*"); //允许跨域的请求头  * 所有请求头都允许

        // 配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", configuration);
        // cors过滤器对象
        return new CorsWebFilter(configurationSource);
    }
}
